// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets;

import java.nio.ByteBuffer;
import zombie.characters.IsoPlayer;
import zombie.characters.skills.PerkFactory;
import zombie.core.logger.LoggerManager;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.network.PacketValidator;
import zombie.network.ServerOptions;
import zombie.network.Userlog;
import zombie.network.packets.hit.Perk;

public class AddXp implements INetworkPacket {
    public final PlayerID target = new PlayerID();
    protected Perk perk = new Perk();
    protected int amount = 0;

    public void set(IsoPlayer _target, PerkFactory.Perk _perk, int _amount) {
        this.target.set(_target);
        this.perk.set(_perk);
        this.amount = _amount;
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        this.target.parse(b, connection);
        this.target.parsePlayer(connection);
        this.perk.parse(b, connection);
        this.amount = b.getInt();
    }

    @Override
    public void write(ByteBufferWriter b) {
        this.target.write(b);
        this.perk.write(b);
        b.putInt(this.amount);
    }

    public void process() {
        if (this.target.player != null && !this.target.player.isDead()) {
            if (this.target.getCharacter() != null && !this.target.getCharacter().isDead()) {
                this.target.getCharacter().getXp().AddXP(this.perk.getPerk(), (float)this.amount, false, false, true);
            }
        }
    }

    @Override
    public boolean isConsistent() {
        return this.target.isConsistent() && this.perk.isConsistent();
    }

    public boolean validate(UdpConnection connection) {
        if (connection.accessLevel != 1 && connection.accessLevel != 2) {
            return true;
        } else if (!connection.havePlayer(this.target.getCharacter())) {
            if (ServerOptions.instance.AntiCheatProtectionType14.getValue() && PacketValidator.checkUser(connection)) {
                PacketValidator.doKickUser(connection, this.getClass().getSimpleName(), "Type14", null);
            }

            return false;
        } else if ((double)this.amount
            > 1000.0 * zombie.SandboxOptions.instance.XpMultiplier.getValue() * ServerOptions.instance.AntiCheatProtectionType15ThresholdMultiplier.getValue()) {
            if (ServerOptions.instance.AntiCheatProtectionType15.getValue() && PacketValidator.checkUser(connection)) {
                PacketValidator.doKickUser(connection, this.getClass().getSimpleName(), "Type15", null);
            }

            return false;
        } else {
            if ((double)this.amount
                > 1000.0
                    * zombie.SandboxOptions.instance.XpMultiplier.getValue()
                    * ServerOptions.instance.AntiCheatProtectionType15ThresholdMultiplier.getValue()
                    / 2.0) {
                LoggerManager.getLogger("user")
                    .write(String.format("Warning: player=\"%s\" type=\"%s\" issuer=\"%s\"", connection.username, "Type15", this.getClass().getSimpleName()));
                PacketValidator.doLogUser(connection, Userlog.UserlogType.SuspiciousActivity, this.getClass().getSimpleName(), "Type15");
            }

            return true;
        }
    }

    @Override
    public String getDescription() {
        String string = "\n\t" + this.getClass().getSimpleName() + " [";
        string = string + "target=" + this.target.getDescription() + " | ";
        string = string + "perk=" + this.perk.getDescription() + " | ";
        return string + "amount=" + this.amount + "] ";
    }
}
